import React, { Component } from "react";
import { Link, Route, Switch } from "react-router-dom";

class Home extends Component {
  render() {
    return <div>home页面</div>;
  }
}

class About extends Component {
  render() {
    return <div>about页面</div>;
  }
}

class Mine extends Component {
  render() {
    return <div>mine页面</div>;
  }
}

class Detail extends Component {
  render() {
    // 如果一个组件是通过路由渲染出来的，可以通过props的方式直接拿到路由信息
    // ?.可选链操作符
    // fn?.()
    console.log(this.props.match?.params?.id);
    // 二次解构的语法
    const {
      params: { id },
    } = this.props.match;
    return <div>detail页面 - {id}</div>;
  }
}

class App extends Component {
  render() {
    return (
      <>
        <h3>react-router-动态路由</h3>
        <ul>
          <li>
            <Link to="/">home</Link>
          </li>
          <li>
            <Link to="/about">about</Link>
          </li>
          <li>
            <Link to="/mine">mine</Link>
          </li>
          <li>
            <Link to="/detail/234">detail</Link>
          </li>
        </ul>

        <hr />

        <Switch>
          <Route path="/" component={Home} exact></Route>
          <Route path="/about" component={About}></Route>
          <Route path="/mine" component={Mine}></Route>
          {/* /:id是动态路由 */}
          <Route path="/detail/:id" component={Detail}></Route>
        </Switch>
      </>
    );
  }
}

export default App;
